#!D:\python\fp1\Scripts\python.exe
#-*- coding:UTF-8 -*-

# CGI处理模块
import cgi, cgitb 
import codecs, sys ,time,os
sys.stdout = codecs.getwriter('utf8')(sys.stdout.buffer)

def writelog (logtxt):
    with open(os.getcwd()+'/logs/log.txt', 'a') as f:
        f.write("\r\n[%s]%s" % (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),logtxt))
    return

# 创建 FieldStorage 的实例化
form = cgi.FieldStorage() 

# 获取数据
user_name = form.getvalue('username')
user_password  = form.getvalue('upassword')

print ("Content-type:text/html")
print ()
print ('<html>')
print ('<head>')
print ('<meta charset="utf-8">')
print ('<title>用户登陆</title>')
print ('</head>')
print ('<body>')
if user_name=="hxwei198":
    print ('<h2>欢迎回来：%s！</h2>' % user_name)
    print ('<p>在这里我们一起来学习Python的CGI开发</p>')
    print ('<br/><a href="start.html">返回首页</a>')
    print ('</body>')
    print ('</html>')
    writelog("用户登陆：(%s)" % user_name)
else:
    print ('<p>不存在该用户，5秒后自动返回登陆页</p>')  
    print ('</body>')
    print ('</html>')
    print('<script type="text/javascript">setTimeout("window.history.back(-1);",1000);</script>')  
    writelog("非法用户：(%s)" % user_name)


